package utils;

import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class URLUtils {

    public static void main(String[] args) throws IOException {
        String url = "http://www.ah.xinhuanet.com/2021-07/15/c_1127654599.htm";
//        String sourceCode = URLUtils.getSourceCode(url);
        String sourceCode = HttpUtil.get(url);
        System.out.println("sourceCode = " + sourceCode);

        //方法一：自己解析
//        String beginIndex = "<title>";
//        String endIndex = "</title>";
//        String title = sourceCode.substring(sourceCode.indexOf(beginIndex) + beginIndex.length(), sourceCode.indexOf(endIndex)).trim();
//        System.out.println("title = " + title);
//
//        String pattern = "[a-zA-z]+://[^\\s]*\\.mp4";
//        Set<String> set = new HashSet<>();
//        Pattern r = Pattern.compile(pattern);
//        Matcher m = r.matcher(sourceCode);
//        while (m.find()) {
//            set.add(m.group().replace("https", "http"));
//        }
//        System.out.println("正则匹配后的地址为: " + set);

        //方法二：Jsoup工具
        Document doc = Jsoup.parse(sourceCode);
        String title = doc.select("title").text();
        System.out.println("title = " + title);
        String videoUrl = doc.select("video").attr("src");
        String videoUrl2 = doc.select("p").attr("video_src");
        System.out.println("videoUrl = " + videoUrl);
        System.out.println("videoUrl2 = " + videoUrl2);
    }

    public static String getSourceCode(String url) throws IOException {
        StringBuilder sb = new StringBuilder();
        InputStream is = URLUtil.getStream(URLUtil.url(url));
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        return sb.toString();
    }
}
